<?php

namespace app\admin\model;

use think\Model;


class Goods extends Model
{

    

    

    // 表名
    protected $name = 'goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'time_text',
        'now_status_text',
        'public_status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getNowStatusList()
    {
        //'1' => __('Now_status 1'), '2' => __('Now_status 2'),
        return [ '3' => __('Now_status 3'), '4' => __('Now_status 4'), '5' => __('Now_status 5'), '6' => __('Now_status 6')];
    }

    public function getPublicStatusList()
    {
        return ['1' => __('Public_status 1'), '2' => __('Public_status 2')];
    }


    public function getTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['time']) ? $data['time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getNowStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['now_status']) ? $data['now_status'] : '');
        $list = $this->getNowStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPublicStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['public_status']) ? $data['public_status'] : '');
        $list = $this->getPublicStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function tag()
    {
        return $this->belongsTo('Tag', 'tag_ids', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
